package top.ningmao.myspring.bean.factory.support;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import top.ningmao.myspring.bean.BeansException;
import top.ningmao.myspring.bean.factory.DisposableBean;
import top.ningmao.myspring.bean.factory.config.BeanDefinition;

import java.lang.reflect.Method;

public class DisposableBeanAdapter implements DisposableBean {
    
    private final Object bean;
    
    private final String beanName;
    
    private final String destroyMethodName;
    
    public DisposableBeanAdapter(Object bean, String beanName, BeanDefinition beanDefinition) {
        this.bean = bean;
        this.beanName = beanName;
        this.destroyMethodName = beanDefinition.getDestroyMethodName();
    }
    @Override
    public void destroy() throws Exception {
        if(bean instanceof DisposableBean){
            ((DisposableBean) bean).destroy();
        }
        
        // 避免同时继承了DisposableBean然后又xml指定了有个销毁方法，且都是destroy导致执行两次
        if (StrUtil.isNotEmpty(destroyMethodName) && !(bean instanceof DisposableBean && "destroy".equals(this.destroyMethodName))) {
            
            // 执行自定义方法
            Method destroyMethod = ClassUtil.getPublicMethod(bean.getClass(),destroyMethodName);
            if(destroyMethod == null){
                throw new BeansException("Couldn't find a destroy method named '" + destroyMethodName + "' on bean with name '" + beanName + "'");
            }
            destroyMethod.invoke(bean);
        }
    }
}
